<template>
  <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="98px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入商品名称" style="width: 150px;"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="商品型号" prop="model">
          <el-input v-model="queryParams.model" placeholder="请输入商品型号" style="width: 150px;"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="采购人姓名" prop="buyName">
          <el-input v-model="queryParams.buyName" placeholder="请输入采购人姓名" style="width: 150px;"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="采购方式" prop="buyType">
          <el-select v-model="queryParams.buyType" placeholder="采购方式" clearable style="width: 130px">
            <el-option v-for="dict in dict.type.buy_type"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否报销" prop="reimburse">
          <el-select v-model="queryParams.reimburse" placeholder="是否报销" clearable style="width: 130px">
            <el-option v-for="dict in dict.type.yes_no"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierId">
          <el-select v-model="queryParams.supplierId" placeholder="供应商名称" clearable style="width: 200px">
            <el-option v-for="dict in dict.type.goods_supplier"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="采购日期" prop="buyDateStr">
          <el-date-picker style="width: 215px;"
                          v-model="queryParams.buyDateStr"
                          @input="buyDateChange"
                          type="daterange"
                          value-format="yyyy-MM-dd"
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
                @sort-change="handleSortChange" :max-height="tableMaxHeight">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="商品名称" align="center" prop="name"/>
        <el-table-column label="商品品牌" align="center" prop="brand"/>
        <el-table-column label="商品型号" align="center" prop="model"/>
        <el-table-column label="商品单位" align="center" prop="unit"/>
        <el-table-column label="含税单价" align="center" prop="costPriceTax"/>
        <el-table-column label="采购数量" align="center" prop="count"/>
        <el-table-column label="库存数量" align="center" prop="remainderCount" />
        <!--      <el-table-column label="采购总价" align="center" prop="cost"/>-->
        <el-table-column label="含税总价" align="center" prop="costTax"/>
        <!--<el-table-column label="有效日期" align="center" prop="validityTerm" width="180" sortable="custom"
                         :sort-orders="['descending','ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.validityTerm, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>-->
        <el-table-column label="采购人姓名" align="center" prop="buyName"/>
        <el-table-column label="采购日期" align="center" prop="buyDate" width="180" sortable="custom"
                         :sort-orders="['descending','ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购方式" align="center" prop="buyType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.buy_type" :value="scope.row.buyType"/>
          </template>
        </el-table-column>
        <el-table-column label="是否报销" align="center" prop="reimburse">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.reimburse"/>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" align="center" prop="supplierName"/>
        <!--<el-table-column label="是否取得发票" align="center" prop="invoice">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.yes_no" :value="scope.row.invoice"/>
          </template>
        </el-table-column>
        <el-table-column label="发票编号" align="center" prop="invoiceNo"/>-->
        <!--            <el-table-column label="备注" align="center" prop="remark"/>-->
        <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-info" v-if="scope.row.count != scope.row.remainderCount"
                       @click="handleView(scope.row)">详情
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.count == scope.row.remainderCount"
                       @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsOrderIn:edit']">修改
            </el-button>
            <el-popconfirm :title="`确认删除 `+scope.row.name+` 数据项？`" v-if="scope.row.count == scope.row.remainderCount"
                           @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                         v-hasPermi="['develop:GoodsOrderIn:remove']">删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>-->
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>

<!--      <goodsOrderIn-form ref="form" @ok="getList"></goodsOrderIn-form>-->
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectOrderSuccess">选 择</el-button>
      <el-button @click="cancelSelect">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {deleteAction, getAction, downloadFile} from '@/api/manage';
    // import GoodsOrderInForm from '@/views/develop/goodsOrderIn/module/GoodsOrderInForm';
    import request from '@/utils/request';

    export default {
        name: "GoodsOrderInListSelectForSale",
        dicts: ['yes_no', 'buy_type', 'goods_supplier'],
        components: {
            // GoodsOrderInForm,
        },
        data() {
            return {
                // 弹出层标题
                title: "选择商品",
                // 是否显示弹出层
                open: false,
                // 遮罩层
                loading: true,
                // 选中数组
                selectRowIds: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 入库信息管理表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 20,
                    name: null,
                    brand: null,
                    unit: null,
                    costPrice: null,
                    count: null,
                    cost: null,
                    validityTerm: null,
                    buyId: null,
                    buyName: null,
                    buyDate: null,
                    buyType: null,
                    reimburse: null,
                    supplierId: null,
                    supplierName: null,
                    params: {
                        selectedIds: "",
                    },
                    orderByColumn: "createTime",
                    isAsc: "desc"
                },
                url: {
                    list: '/goodsOrderIn/goodsOrderIn/list',
                    remove: '/goodsOrderIn/goodsOrderIn/',
                    export: '/goodsOrderIn/goodsOrderIn/export',
                },
                buyDateStr: "",
                selectRows: [],
                tableMaxHeight: window.innerHeight - 400,
            };
        },
        created() {
            // this.getList();
        },
        methods: {
            selectOrderSuccess(){
                let that = this;
                that.$emit('ok', that.selectRows);
                that.open = false;
            },
            cancelSelect() {
                this.open = false;
                this.selectRows = [];
            },
            //打开选择商品的列表,并将已存在商品id接收过来,作为过滤条件
            showSelectOrderDialog(selectedIds) {
                let that = this;
                that.open = true;
                // that.queryParams.buyId = that.$store.getters.userId;
                // that.queryParams.buyName = that.$store.getters.nickName;
                that.queryParams.params.dataFlag = "1";
                // let selectIds = JSON.stringify(selectedIds);
                // selectIds = selectIds.substring(1, selectIds.length-1);
                that.queryParams.params.selectedIds = selectedIds;

                that.getList();
            },
            handleSortChange(column) {
                let that = this;
                that.queryParams.orderByColumn = column.prop;
                that.queryParams.isAsc = column.order;
                that.getList();
            },
            buyDateChange(val) {
                let that = this;
                that.$nextTick(() => {
                    if (that.queryParams.buyDateStr != null && that.queryParams.buyDateStr.length > 0) {
                        that.queryParams.buyDateStr = null;
                        that.$set(that.queryParams, "buyDateStr", [val[0], val[1]]);
                        that.queryParams.params.buyDateBeg = val[0];
                        that.queryParams.params.buyDateEnd = val[1];
                    } else {
                        this.queryParams.buyDateStr = null;
                        this.queryParams.params.buyDateBeg = null;
                        this.queryParams.params.buyDateEnd = null;
                    }
                });
            },
            /** 查询入库信息管理列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.queryParams.params.buyDateBeg = null;
                this.queryParams.params.buyDateEnd = null;
                this.queryParams.orderByColumn = "createTime";
                this.queryParams.isAsc = "desc";
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.selectRows = selection;
                this.selectRowIds = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            handleView(row) {
                this.$refs.form.handleView(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const selectRowIds = row.id || this.selectRowIds;
                deleteAction(this.url.remove, selectRowIds).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                let that = this;
                that.loading = false;
                that.$confirm('是否确认导出商品入库信息表?', "确认", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    downloadFile('/goodsOrderIn/goodsOrderIn/exportNormal', that.queryParams);
                });
            },
        }
    };
</script>
